var fMessage = new i18nMessage('../../servlet/I18nMessageServlet?keyPrefix=ui.factory');
var message = new i18nMessage('../../servlet/I18nMessageServlet?keyPrefix=message');
var ctYesno = new i18nMessage('../../servlet/CodeTableServlet?name=CtYesno');
var ctProductFamily = new i18nMessage('../../servlet/CodeTableServlet?name=CtFactoryType');
var factoryStore;
var factoryGrid;
var factoryWindow;
var getSelectedFactory;
var selectFactory = function(selectFactoryCallback, ctFactoryType){
	var fcUrl = '../../servlet/BiFactoryServlet?action=4';
	if(ctFactoryType){
		fcUrl += '&ctFactoryType=' + ctFactoryType;
	}
    if (!factoryWindow) {
        factoryWindow = new Ext.Window({
            title: message.get('message.combo.pleaseselect'),
            layout: 'fit',
            width: 900,
            height: 605,
            closeAction: 'hide',
            plain: true,
            modal: true,
            items: [{
                id: 'factoryPanel',
                region: 'center',
                bodyStyle: {
                    background: '#ffffff',
                    padding: '7px'
                },
                html: '<div id="factoryDiv"></div>'
            }]
        });
    }
    factoryWindow.show();
    
    if (factoryGrid){
    	factoryStore.proxy = new Ext.data.HttpProxy({
			url : fcUrl
		});
		factoryStore.reload();
		return;
	}
    factoryStore = new Ext.data.Store({
        url: fcUrl,
        
        reader: new Ext.data.XmlReader({
            record: 'item',
            id: 'id',
            totalRecords: 'itemTotal'
        }, [ 
        'index', 'id', 'nameCN','nameEN', 'departmentCN','departmentEN', 'ctFactoryType', 'labour', 'comment',
		'addressCommercialCN','addressCommercialEN', 'commercialContactCN','commercialContactEN', 'commercialPhone', 'commercialEmail', 'commercialFax',
		'addressFactoryCN','addressFactoryEN', 'factoryContactCN','factoryContactEN', 'factoryPhone', 'factoryEmail', 'factoryFax',
		'hasLoom', 'loomJauge', 'loomQuality', 'loomLabour',
		'hasKnitting', 'knittingJauge', 'knittingQuality', 'knittingLabour',
		'hasElectronic', 'electronicJauge', 'electronicQuality', 'electronicLabour',
		'hasIntarsia', 'intarsiaJauge', 'intarsiaQuality', 'intarsiaLabour',
		'isCarded', 'isSemiWorsted', 'isWorsted', 'isSpinningOther1', 'isSpinningOther2', 'isSpinningOther3',
		'isFibre', 'isHank', 'isBobbin', 'isSpace', 'isDyeingOther1', 'isDyeingOther2', 'isDyeingOther3',
		'isRaw', 'isSolid', 'isChine', 'isTwisted', 'isFancy', 'isYarnOther1', 'isYarnOther2', 'isYarnOther3',
		'isCotton', 'isMercerisedCotton', 'isKapok', 'isLinen', 'isHemp', 'isRamie', 'isWool', 'isCashmere', 'isYack', 'isCamel', 'isSilk', 'isViscose', 'isModal', 'isModal', 'isTencel', 'isBamboo', 'isMilkFibre', 'isAcetate', 'isTriacetate', 'isPolyamid', 'isPolyester', 'isAcrylic', 'isMaterialOther1', 'isMaterialOther2', 'isMaterialOther3', 'isMaterialOther4', 'isMaterialOther5',
		'isNeedle', 'isHand', 'isIntarsiaHand', 'isElectronic', 'isEquipmentOther1', 'isEquipmentOther2', 'isEquipmentOther3',
		'needleCapacity',
		'isHandJauge3', 'isHandJauge5', 'isHandJauge7', 'isHandJauge9', 'isHandJauge12', 'isHandJauge14', 'isHandJauge16', 'isHandJauge18',
		'handJauge3Capacity', 'handJauge5Capacity', 'handJauge7Capacity', 'handJauge9Capacity', 'handJauge12Capacity', 'handJauge14Capacity', 'handJauge16Capacity', 'handJauge18Capacity',
		'isIntarsiaHandJauge3', 'isIntarsiaHandJauge5', 'isIntarsiaHandJauge7', 'isIntarsiaHandJauge9', 'isIntarsiaHandJauge12', 'isIntarsiaHandJauge14', 'isIntarsiaHandJauge16', 'isIntarsiaHandJauge18',
		'intarsiaHandJauge3Capacity', 'intarsiaHandJauge5Capacity', 'intarsiaHandJauge7Capacity', 'intarsiaHandJauge9Capacity', 'intarsiaHandJauge12Capacity', 'intarsiaHandJauge14Capacity', 'intarsiaHandJauge16Capacity', 'intarsiaHandJauge18Capacity',
		'isElectronicJauge3', 'isElectronicJauge5', 'isElectronicJauge7', 'isElectronicJauge9', 'isElectronicJauge12', 'isElectronicJauge14', 'isElectronicJauge16', 'isElectronicJauge18',
		'electronicJauge3Capacity', 'electronicJauge5Capacity', 'electronicJauge7Capacity', 'electronicJauge9Capacity', 'electronicJauge12Capacity', 'electronicJauge14Capacity', 'electronicJauge16Capacity', 'electronicJauge18Capacity',
		'isRemeshing', 'isFulling', 'isIroning', 'isQualityControl', 'isBlendProcessOther1', 'isBlendProcessOther2',
		'remeshingPD', 'fullingPD', 'ironingPD', 'qualityControlPD', 'blendProcessOther1PD', 'blendProcessOther2PD',
		'isJersey', 'isFleece', 'isCoat', 'isInterlock', 'isHuitSerrures', 'isSimpleJacquard', 'isDoubleJacquard', 'isKnittingFamilyOther1', 'isKnittingFamilyOther2',
		'isJerseyJauge14', 'isJerseyJauge16', 'isJerseyJauge18', 'isJerseyJauge20', 'isJerseyJauge22', 'isJerseyJauge24', 'isJerseyJauge28', 'isJerseyJauge32',
		'isFleeceJauge14', 'isFleeceJauge16', 'isFleeceJauge18', 'isFleeceJauge20', 'isFleeceJauge22', 'isFleeceJauge24', 'isFleeceJauge28', 'isFleeceJauge32',
		'isButton', 'isZip', 'isEtiquette', 'isGriffes', 'isHangtags', 'isCuir', 'isAccessoryTypeOther1', 'isAccessoryTypeOther2', 'isAccessoryTypeOther3']),
        
        remoteSort: true
    });
    
    factoryStore.setDefaultSort('id', 'desc');
    
    getSelectedFactory = function(index){
		selectedFactory = factoryStore.getAt(index).data;
    }
    
    function renderSelectItem(value, p, record, rowIndex){
        return String.format('<a href="javascript: getSelectedFactory(\'{2}\');selectFactoryCallback(\'{0}\', \'{1}\', \'{2}\');factoryWindow.hide();"><img src="../../images/icons/arrow_rotate_clockwise.png" title="' + message.get('message.list.select') + '" /></a>', record.data.id, record.data.nameCN, rowIndex);
    }
	
	var factoryFilters = new Ext.grid.GridFilters({
					  filters:[
					    {type: 'string',  dataIndex: 'nameCN'},
					    {type: 'string',  dataIndex: 'nameEN'},
					    {type: 'string', dataIndex: 'departmentCN'},
					    {type: 'string', dataIndex: 'departmentEN'},
					    {
						      type: 'list',  
						      dataIndex: 'ctProductFamily', 
						      options: ctProductFamily.getCtArray()
					    }
					]});
    // create the grid
    factoryGrid = new Ext.grid.GridPanel({
        title: fMessage.get('ui.factory.listtitle'),
        store: factoryStore,
        stripeRows: true,
        plugins: [factoryFilters],
        height: 560,
        columns: [{
            header: message.get('message.list.id'),
            width: 20,
            dataIndex: 'index',
            sortable: false
        }, {
            header: fMessage.get('ui.factory.name')+'(CN)',
            dataIndex: 'nameCN',
            sortable: true
        }, {
            header: fMessage.get('ui.factory.name')+'(FR/EN)',
            dataIndex: 'nameEN',
            sortable: true
        }, {
            header: fMessage.get('ui.factory.department') + '(CN)',
            width: 50,
            dataIndex: 'departmentCN',
            sortable: true
        }, {
            header: fMessage.get('ui.factory.department') + '(FR/EN)',
            width: 50,
            dataIndex: 'departmentEN',
            sortable: true
        }, {
            header: fMessage.get('ui.factory.type'),
            width: 50,
            dataIndex: 'ctFactoryType',
            sortable: true
        }, {
            header: message.get('message.list.select'),
            width: 30,
            dataIndex: 'id',
            sortable: false,
            renderer: renderSelectItem
        }],
        viewConfig: {
            forceFit: true
        },
        loadMask: true,
        renderTo: 'factoryDiv',
        bbar: new Ext.PagingToolbar({
            pageSize: 20,
            store: factoryStore,
            displayInfo: true
        })
    });
    
    var startPage = getPrarmeterValue(window.location, 'startPage');
    var start;
    if (startPage == null || startPage == '') {
        start = 0;
    }
    else {
        start = (startPage - 1) * 20;
    }
    factoryStore.load({
        params: {
            start: start,
            limit: 20
        }
    });
}


